package com.darker.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.darker.reggie.common.R;
import com.darker.reggie.entity.Employee;
import com.darker.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
@Slf4j
@RequestMapping("/employee")
@RestController
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    //员工登录
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {

        //第一步 获取密码， 把密码转换为md5
        String password = employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());

        //第二步 从数据库根据用户名查找数据
        LambdaQueryWrapper<Employee> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(wrapper);

        //第三步 没查到结果返回失败
        if(emp==null){
           return R.error("该用户没有找到");
        }

        //第四步 进行密码的比对 如果不一致返回失败
        if(!emp.getPassword().equals(password)){
            return R.error("密码错误");
        }

        //第五步 如果员工状态被禁用返回失败

        if(emp.getStatus()==0){
            return R.error("该账号被禁用");
        }
        request.getSession().setAttribute("employee",emp.getId());

        return R.success(emp);
    }

    //退出登录
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清楚session的数据
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    //新增员工
    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpServletRequest request){

        //给新增员工设置初始密码  为其MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        //赋值创建时间 更新时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        //设置本次操作人的id 通过session获得
//        long empid = (long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(empid);
//        employee.setUpdateUser(empid);

        boolean save = employeeService.save(employee);
        if(save){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }
    //分页查询
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        log.info("page = {} , pageSize = {} , name ={}",page,pageSize,name);
       //构造分页构造器
        Page page1=new Page(page,pageSize);
       //条件构造器
       LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
       //参数  第一个判断name是否有值后执行  第二个是字段 第三个是参数
       queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
       //添加排序条件  按照更新时间排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        employeeService.page(page1,queryWrapper);
        return R.success(page1);
    }
    //更新by—id
    @PutMapping
    public R<String> updateByid(@RequestBody Employee employee,HttpServletRequest request){
//        Long userid = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
//      employee.setUpdateUser(userid);
      employeeService.updateById(employee);
    return R.success("更新成功");
    }
    //根据id查询员工信息
    @GetMapping("/{id}")
    public R<Employee> selectById(@PathVariable long id){
        log.info("{}",id);
        Employee employee = employeeService.getById(id);
        if(employee!=null){
            return R.success(employee);
        }else {
            return R.error("根据Id查询失败");
        }

    }
}
